<template src="./template.html"></template>

<script>
/* global moment */
import ResizeMixin from 'mixins/resize'
import { PAGE_CONFIG } from 'common/consts';
import { COL_INDEXS } from './consts';
import { EXPIRED_STATUS, EXPIRED_STATUS_SIMPLIFY } from '../consts'
import { getUser, deleteUser as deleteUserApi } from './models';

export default {
  mixins: [ResizeMixin],
  data() {
    return {
      COL_INDEXS,
      EXPIRED_STATUS,
      EXPIRED_STATUS_SIMPLIFY,
      isPageLoading: false,

      tableList: [],
      PAGE_CONFIG: PAGE_CONFIG,
      pageNo: PAGE_CONFIG.defaultPageNo,
      pageSize: PAGE_CONFIG.defaultPageSize,
      totalCount: 0,
      filterForm: {
        enabled: '',
        q: ''
      },
    };
  },
  computed: {
    userInfo() {
      return this.$store.state.user.userInfo
    }
  },

  methods: {
    indexMethod(index) {
      return (this.pageNo - 1) * this.pageSize + index + 1;
    },
    $search() {
      this.pageNo = 1
      this.$getTableList()
    },
    $handleAdd() {
      this.$router.push('/users/manager/add')
    },
    $handleEdit(row) {
      this.$router.push(`/users/manager/edit/${row.id}`)
    },
    $handleDelete(row) {
      this.$confirm('你确定要删除该用户吗？', {
        type: 'seriousWarning',
        confirmButtonText: '删除',
      })
        .then(() => {
          this.deleteUser(row);
        })
        .catch(() => {});
    },

    async deleteUser(row) {
      this.isPageLoading = true;
      const params = {
        ids: [row.id],
      };
      try {
        await deleteUserApi(params);
        this.isPageLoading = false;
        this.$message.success('用户信息删除成功！')
        this.$getTableList();
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.retDesc || err.message || '删除用户异常')
      }
    },
    async $getTableList() {
      this.isPageLoading = true;
      const params = {
        ...this.filterForm,
        limit: this.pageSize,
        page: this.pageNo,
      };
      try {
        const { data } = await getUser(params);
        console.log(data)
        this.tableList = (data.list || []).map(it => ({
          ...it,
          createTime: it.createTime && moment(it.createTime).format('YYYY-MM-DD HH:mm')
        }));
        this.totalCount = data.total
        this.isPageLoading = false;
      } catch (err) {
        this.isPageLoading = false;
        this.$message.error(err.retDesc || err.message || '获取用户列表出错')
      }
    },
    $handlePageChange() {
      this.$getTableList();
    },
  },
  created() {
    this.$getTableList();
  },
};
</script>
